//
// 待审批事项
//
<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div class="main_main">
      <div v-if="itenindex == 0" class="SearchBar_main main_main_search">
        <div class="main_main_search_item">
          <div class="name">单据类型</div>
          <div class="value">
            <el-select v-model="pageData.title" filterable @clear="getList()" @change="search">
              <el-option
                v-for="item in titleOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
        </div>
        <div class="main_main_search_item">
          <div class="name">单据编码</div>
          <div class="value">
            <el-input v-model="pageData.businessCode" clearable @clear="getList()" @keyup.enter.native="getList()" />
          </div>
        </div>
        <div class="main_main_search_item">
          <div class="name">发起人</div>
          <div class="value">
            <el-input v-model="pageData.userName" clearable @clear="getList()" @keyup.enter.native="getList()" />
          </div>
        </div>
        <div class="main_main_search_item">
          <div class="name">发起时间</div>
          <div class="value">
            <TimeRangeSearch :band-value.sync="pageData.sendTime" :clearable="pageData.sendTime" @clearData="getList()" />
          </div>
        </div>
        <div v-if="!loading" class="main_main_search_item last_item">
          <a class="button" @click.stop="search">
            搜索
          </a>
          <a class="button" @click.stop="refresh">
            刷新
          </a>
        </div>
      </div>
      <div v-else class="SearchBar_main main_main_search">
        <div class="main_main_search_item">
          <div class="name">状态</div>
          <div class="value">
            <el-select v-model="pageData.businessStatus" filterable @clear="getList()" @change="search">
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
        </div>
        <div class="main_main_search_item">
          <div class="name">单据类型</div>
          <div class="value">
            <el-select v-model="pageData.title" filterable @clear="getList()" @change="search">
              <el-option
                v-for="item in titleOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
        </div>
        <div class="main_main_search_item">
          <div class="name">单据编码</div>
          <div class="value">
            <el-input v-model="pageData.businessCode" clearable @clear="getList()" @keyup.enter.native="getList()" />
          </div>
        </div>
        <div class="main_main_search_item">
          <div class="name">审批时间</div>
          <div class="value">
            <TimeRangeSearch :band-value.sync="pageData.endTime" :clearable="pageData.endTime" @clearData="getList()" />
          </div>
        </div>
        <div v-if="!loading" class="main_main_search_item last_item">
          <a class="button" @click.stop="search">
            搜索
          </a>
          <a class="button" @click.stop="refresh">
            刷新
          </a>
        </div>
      </div>
      <div class="main_main_btn">
        <div class="main_btn_read" :class="{active : itenindex == '0'}" @click="btnClick(1)">
          <span>待处理</span>
        </div>
        <div class="main_btn_unread" :class="{active : itenindex == '1'}" @click="btnClick(2)">
          <span>已处理</span>
        </div>
      </div>
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <template slot="empty">
            <el-image
              style="width: 120px; height: 100px; margin-left: calc(50% - 65px)"
              :src="imageUrl"
              fit="fill"
            />
            <div>
              暂无审批任务
            </div>
          </template>
          <el-table-column type="selection" width="70" align="center" />
          <!-- 已处理的时候才显示          -->
          <el-table-column
            v-if="itenindex == '1'"
            label="审批结果"
            min-width="90"
          >
            <template slot-scope="scope">
              <StatusColum :status-name="scope.row.businessStatusName" />
            </template>
          </el-table-column>
          <Column
            v-for="(column,index) in tableColumns"
            :key="index"
            :prop="column.prop"
            :is-show="column.isShow"
            :label="column.label"
            :sortable="column.sortable"
            :width="column.width"
            :min-width="column.minWidth"
            :align="column.align"
          />
          <!-- 已处理的时候才显示          -->
          <el-table-column
            v-if="itenindex == '1'"
            label="审批时间"
            min-width="140"
          >
            <template slot-scope="scope">
              <div>
                <span class="gray_color" />
                <div>{{ scope.row.endTime }}</div>
              </div>
            </template>
          </el-table-column>
          <!-- 已处理的时候才显示          -->
          <el-table-column v-if="itenindex == '1'" label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import imageUrl from '@/assets/images/backlogTask/待审批暂无数据.jpg'
import { listForSaleCollect, read, remove } from '@/api/backlogTask/backlogTask'
import StatusColum from '../../saleManage/saleSend/StatusColum'
import { mapGetters } from 'vuex'
export default {
  name: 'ByMaterial',
  components: { StatusColum },
  computed: {
    ...mapGetters([
      'notificationChangeTag'
    ])
  },
  watch: {
    notificationChangeTag() {
      this.getList()
    }
  },
  data() {
    const newVar = {
      imageUrl: imageUrl,
      loading: true,
      pageId: '待审批事项', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'sendTime',
        isAsc: 'desc',
        type: '3',
        listType: '4'
      },
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'menuName', label: '单据类型', canOrder: false },
        { prop: 'businessCode', label: '单据编码', canOrder: false },
        { prop: 'userName', label: '发起人', canOrder: false },
        { prop: 'deptName', label: '所属部门', canOrder: false },
        { prop: 'sendTime', label: '发起时间', minWidth: 140, sortable: 'custom', canOrder: false }
      ],
      defaultSearchColumns: [
        { prop: 'title', label: '单据类型', type: 'select', optionsName: 'titleOptions', isIndex: 1 },
        { prop: 'businessCode', label: '单据编码', type: 'input', isIndex: 1 },
        { prop: 'userName', label: '发起人', type: 'input', isIndex: 1 },
        { prop: 'sendTime', label: '发起时间', clearable: true, type: 'time', isTime: false, isIndex: 1 }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '批量审批', perm: '' }
        // { label: '批量删除1', perm: '' }
      ],
      titleOptions: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: '销售订单', label: '销售订单' },
        { value: '销售发货单', label: '销售发货单' }
      ],
      statusOptions: [
        { value: undefined, label: '全部' },
        { value: 1, label: '已通过' },
        { value: 2, label: '已驳回' }
      ], // 单据类型
      itenindex: '0' // 待处理/已处理状态
    }
    return newVar
  },
  mounted() {
  },
  methods: {
    search() {
      this.pageData.pageNum = 1
      this.getList()
    },
    refresh() {
      this.pageData = {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'sendTime',
        isAsc: 'desc',
        type: '3',
        listType: '4'
      }
      this.getList()
    },
    cellStyle({ row, column }) {
      if (column.property == 'businessCode') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      if (column.property == 'businessCode') {
        if (row.menuPermission === '0' && row.title != '系统通知' && row.title !== '系统公告' && row.title !== '企业公告') {
          this.$message.warning('您没有当前页面的权限,请联系管理员')
        } else {
          read({ ids: row.id }).then(res => {
            this.$store.state.order.contendId = row.contentId
            this.$store.dispatch('notification/changeId', row.contentId)
            this.$router.push('/' + row.menuComponent)
            this.$store.dispatch('notification/changeDataStatus', row.menuStatus)
            this.$parent.$parent.$parent.getInfoMsgNum()
          })
        }
      }
    },
    btnClick(index) { // 待处理/已处理-切换
      if (!index) { // 状态没有的话 默认等于/待处理
        this.itenindex = '0'
      }
      if (index == '1') {
        this.itenindex = '0'// 等于待处理
        this.getList()
      } else {
        this.itenindex = '1' // 等于已处理
        this.getList()
      }
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.selectionsAll = []
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    },
    getList() {
      listForSaleCollect({ ...this.pageData, executeStatus: this.itenindex }).then(res => {
        this.tableData = res.data // 表单数据
        this.total = res.total // 分页
        this.loading = false // loading
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight - 15 // 表格高度自动计算
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.table_container {
  margin-top: 10px !important;
}
.main_header{
  .padding_container{
    justify-content: flex-end;
  }
}
.active {
  height: 100% !important;
  background: rgba(56, 123, 253, 1) !important;
  color: #fff !important;
  border: none !important;
  font-size: 14px !important;
}
.main_main_btn {
  display: flex;
  height: 30px;
  width: 100%;
  padding-left: 29px;
  margin-top: 20px;
  .main_btn_read {
    width: 85px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid rgba(228, 228, 228, 1);
    border-radius:4px 0px 0px 4px;
    font-size: 14px;
    color: #999999;
    cursor: pointer
  }
  .main_btn_unread {
    width: 85px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid rgba(228, 228, 228, 1);
    border-left: none;
    border-radius:0px 4px 4px 0px;
    font-size: 14px;
    cursor: pointer;
    color: #999999;
  }
}
</style>
